extends Node2D

export(PackedScene) var zidan_prefab

export var shoot_speed = 20000

const speed := 10


export(float) var nega = 1.0
export(bool) var camera = false

onready var ren: RigidBody2D = $"人"
onready var da: RigidBody2D = $"大"
onready var dian: RigidBody2D = $"电"
onready var an: RigidBody2D = $"俺"

onready var animation_player: AnimationPlayer = $"俺/AnimationPlayer"
onready var current_camera: Camera2D = $"俺/Camera2D"

var ren_dead = false 
var da_dead = false
var dian_dead = false

var huxi_time = 0

func _ready() -> void:
	current_camera.current = camera

func set_ren_dead(b):
	ren.gravity_scale = 100
	# ren.set_collision_layer_bit(0, true)
	if da_dead:
		if not dian_dead:
			animation_player.play("dian")
	else:
		if not dian_dead:
			animation_player.play("dadian")
		else:
			animation_player.play("da")
	ren_dead =  b
	
func set_da_dead(b):
	da.gravity_scale = 100
	# da.set_collision_layer_bit(0, true)
	if ren_dead:
		if not dian_dead:
			animation_player.play("dian")
	else:
		if not dian_dead:
			animation_player.play("rendian")
		else:
			animation_player.play("ren")
	da_dead =  b
	
func set_dian_dead(b):
	dian.gravity_scale = 100
	# dian.set_collision_layer_bit(0, true)
	if da_dead:
		if not ren_dead:
			animation_player.play("ren")
	else:
		if not ren_dead:
			animation_player.play("renda")
		else:
			animation_player.play("da")
	dian_dead = b

func _input(event: InputEvent) -> void:
	if event.is_action_pressed("shoot"):
		if zidan_prefab:
			var zidan: RigidBody2D = zidan_prefab.instance()
			get_parent().add_child(zidan)
			zidan.global_position = an.global_position
			zidan.linear_velocity = an.linear_velocity
			var zidan_direction = Vector2.ZERO
			if Input.is_action_pressed("move_up"):
				zidan_direction += Vector2.UP
			if Input.is_action_pressed("move_left"):
				zidan_direction += Vector2.LEFT
			if Input.is_action_pressed("move_right"):
				zidan_direction += Vector2.RIGHT
			if Input.is_action_pressed("move_down"):
				zidan_direction += Vector2.DOWN

			if zidan_direction == Vector2.ZERO:
				zidan_direction = Vector2.RIGHT

			zidan.linear_velocity += zidan_direction * shoot_speed
			an.linear_velocity.x = - zidan.linear_velocity.x * 0.1
				
	elif event.is_action_pressed("cursor_shoot"):
		var zidan: RigidBody2D = zidan_prefab.instance()
		get_parent().add_child(zidan)
		zidan.global_position = an.global_position
		
		# 坐标变换
		var relative_center_pos = -(get_viewport().get_visible_rect().size / 2) + event.position
		relative_center_pos *= current_camera.zoom
		var cursor_position = current_camera.get_camera_screen_center() + relative_center_pos
	
		var cursor_direction = an.global_position.direction_to(cursor_position)
		zidan.linear_velocity = cursor_direction * shoot_speed
		
		an.linear_velocity.x = - zidan.linear_velocity.x * 0.1

func _process(delta: float) -> void:
	
	if ren_dead and da_dead and dian_dead:
		# 死亡
		Globals.reload_scene()

	var ren_position = ren.global_position 
	var da_position = da.global_position
	var dian_position = dian.global_position
	
	if an.linear_velocity.length_squared() > 1000:
		
		# 走路运动
		ren_position += Vector2(0, min(abs(ren.rotation) * 30000, 300))
		da_position += + Vector2(0, min(abs(da.rotation) * 30000, 300))
		dian_position += + Vector2(0, min(abs(dian.rotation * 30000), 300))
	else:
		
		# 呼吸运动
		huxi_time += delta
		ren_position.x += 2000 * sin(huxi_time) * delta
		da_position.y += 2000 * sin(huxi_time) * delta
		dian_position.y -= 2000 * sin(huxi_time) * delta

	
	if not ren_dead:
		ren.linear_velocity = (an.global_position - ren_position) * speed
		ren.angular_velocity = -ren.rotation * speed
		
	if not da_dead:
		if dian_dead:
			da_position.y -= 400
		da.linear_velocity = (an.global_position - da_position) * speed
		da.angular_velocity = -da.rotation * speed
	
	if not dian_dead:
		dian.linear_velocity = (an.global_position - dian_position) * speed
		dian.angular_velocity = -dian.rotation * speed
	
	
	
	
	var vec1 = an.global_position - ren.global_position if not ren_dead else Vector2.ZERO
	var vec2 = an.global_position - da.global_position if not da_dead else Vector2.ZERO
	var vec3 = an.global_position - dian.global_position if not dian_dead else Vector2.ZERO
	
	# (vec1.length_squared())
	
	if vec1.length_squared() > 3500000:
		an.linear_velocity = vec1.normalized() * 8000
		set_ren_dead(true)
	if vec2.length_squared() > 3500000:
		an.linear_velocity = vec2.normalized() * 8000
		set_da_dead(true)
	if vec3.length_squared() > 3500000:
		an.linear_velocity = vec3.normalized() * 8000
		set_dian_dead(true)
	
	var v = (vec1 + vec2 + vec3)
	an.qianyin_force = -v * pow(v.length_squared() / 1000000, 3) / (1 + int(not ren_dead) + int(not da_dead) + int(not dian_dead))


func _on_ren_body_entered(body: Node) -> void:
	if body.name.ends_with('!') or body.name == "人":
		print(body.name)
		set_ren_dead(true)


func _on_da_body_entered(body: Node) -> void:
	if body.name.ends_with('!') or body.name == "大":
		print(body.name)
		set_da_dead(true)


func _on_dian_body_entered(body: Node) -> void:
	if body.name.ends_with('!') or body.name == "电":
		print(body.name)
		set_dian_dead(true)
